using System.Collections.Generic;

namespace ColladaDotNet.OpenTKMapping
{
	public class Animation:DocumentLibraryElement<animation>
	{
		private List<Source> sources = new List<Source>();

		private List<Channel> channels = new List<Channel>();

		private List<Sampler> samplers = new List<Sampler>();

		private List<Animation> animations = new List<Animation>();

		internal override void Load(animation colladaData)
		{
			base.Load(colladaData);

			foreach (var item in colladaData.Items)
			{
				var i = Document.CreateElement(item);
				if (i != null)
				{
					if (i is Source)
						sources.Add((Source)i);
					else if (i is Channel)
						channels.Add((Channel)i);
					else if (i is Sampler)
						samplers.Add((Sampler)i);
					else if (i is Animation)
						animations.Add((Animation)i);
				}
			}
			//source = Source.CreateSource(colladaData);
			//source.Document = this.Document;
			//foreach (var item in colladaData.Items)
			//{
			//    source.Load(colladaData);
			//}
		}

		public void Apply(double time)
		{
			foreach (var channel in channels)
			{
				channel.Apply(time);
			}
		}

		public double[] GetFrames()
		{
			SortedDictionary<double, bool> d = new SortedDictionary<double, bool>();
			foreach (var sampler in samplers)
			{
				var time = sampler.InputTime;
				for (ulong i=0; i<time.Count; ++i)
				{
					d[time.GetDouble(i)] = true;
				}
			}
			double[]res = new double[d.Keys.Count];
			d.Keys.CopyTo(res,0);
			return res;
		}
	}
}